let username = $("#username");
let pwd = $("#password");
// 验证用户名
function checkUsername() {
  let tip = username.next();
  $.ajax({
    url: "http://localhost:8080/test",
    data: { username: username.val(), target: "checkUser" },
    type: "POST",
    dataType: "json",
    success: function (data) {
      if (data.res === "ok") {
        tip.text("用户名验证成功");
        username.css("borderColor", "green");
        tip.addClass("ok");
        tip.removeClass("error");
      } else {
        tip.text("用户名已重复");
        username.css("borderColor", "red");
        tip.addClass("error");
        tip.removeClass("ok");
      }
    },
    timeout: 3000,
    error: function () {
      tip.text("网站请求出错");
      username.css("borderColor", "red");
      tip.addClass("error");
      tip.removeClass("ok");
    },
  });

}

// 验证输入密码
function checkPwd() {
  // 密码必须由数字、字母两种字符组成，长度在8-15位之间
  let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,15}$/;
  let tip = pwd.next();
  if (reg.test(pwd.val())) {
    tip.text("密码强度合格");
    pwd.css("borderColor", "green");
    tip.addClass("ok");
    tip.removeClass("error");
    return true;
  } else {
    tip.text("密码强度过低");
    pwd.css("borderColor", "red");
    tip.addClass("error");
    tip.removeClass("ok");
    return false;
  }
}

// 验证重复输入密码
function checkRePwd() {
  let repwd = $("#repwd");
  let tip = repwd.next();
  if (repwd.val() === pwd.val()) {
    tip.text("重复输入正确");
    repwd.css("borderColor", "green");
    tip.addClass("ok");
    tip.removeClass("error");
    return true;
  } else {
    tip.text("密码输入不一致");
    repwd.css("borderColor", "red");
    tip.addClass("error");
    tip.removeClass("ok");
    return false;
  }
}

// 验证邮箱
function checkEmail() {
  let reg = /^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/;
  let email = $("#email");
  let tip = email.next();
  if (reg.test(email.val())) {
    tip.text("邮箱格式正确");
    email.css("borderColor", "green");
    tip.addClass("ok");
    tip.removeClass("error");
    return true;
  } else {
    tip.text("邮箱格式错误");
    email.css("borderColor", "red");
    tip.addClass("error");
    tip.removeClass("ok");
    return false;
  }
}